//
//  CommodityDetailMore.m
//  DuBai
//
//  Created by darktm on 14-3-1.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "CommodityDetailMore.h"

#import "ShareSDKManage.h"

#import "TaskWithGetGoodsTechnology.h"
#import "TaskWithFavourite.h"
#import "TaskWithShare.h"
#import "TaskWithGetGoodsComments.h"

#import "GoodsDetail.h"
#import "Comments.h"


@interface CommodityDetailMore ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSString* Technology;
@property (nonatomic,strong)GoodsDetail *goodDetail;
@property(nonatomic,strong)NSString* comments;
@end


@implementation CommodityDetailMore

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    [MBProgressHUD hideHUDForView:self.superview animated:YES];
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetGoodsTechnology class]]){
        self.Technology = rsult;
        [self refreshBrand];
    }else if ([obj isKindOfClass:[TaskWithFavourite class]]){
        self.favourateBtn.enabled = NO;
    }else if ([obj isKindOfClass:[TaskWithShare class]]){
//        NSString *shareUrl = rsult;
        
        [ShareSDKManage shareInstance].shareImage = nil;
        [[ShareSDKManage shareInstance] showActionSheet];
        
    }else if ([obj isKindOfClass:[TaskWithGetGoodsComments class]]){
        self.comments = rsult;
        
        [self configureComments];
    }
    
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideHUDForView:self.superview animated:YES];
    DLog(@"%@",obj.networkOp.url);
}


#pragma mark - customer method -

-(void)configureInfo:(id)values{
    if([values isKindOfClass:[GoodsDetail class]]){
        self.goodDetail = values;
        [self refreshGoodIntro];
    }
}

-(void)configureComments{
    
    
    
    
    [self refreshComments];
}

//商品简介
-(void)refreshGoodIntro{
    
    if(1){//如果登录了就判断是否收藏过
        if([self.goodDetail.hasFavorites intValue] == 1){
            self.favourateBtn.enabled = NO;
        }
    }else{
            self.favourateBtn.enabled = NO;
    }
    
    @try {
        NSString *htmlHeader = @"<html><body>";
        
        NSMutableString *htmlBody = [NSMutableString string];
        [htmlBody appendString:@"<br>&nbsp;&nbsp;&nbsp;&nbsp;."];
        [htmlBody appendString:self.goodDetail.desc];
        [htmlBody appendString:@"<br>&nbsp;&nbsp;&nbsp;&nbsp;."];
        [htmlBody appendString:self.goodDetail.material];
        [htmlBody appendString:@"<br>&nbsp;&nbsp;&nbsp;&nbsp;."];
        [htmlBody appendString:self.goodDetail.size];
        [htmlBody appendString:@"<br>&nbsp;&nbsp;&nbsp;&nbsp;."];
        [htmlBody appendString:self.goodDetail.placeOfOrigin];
        
        NSString *htmlFooter = @"</body>    </html>";
        
        NSMutableString *html = [NSMutableString string];
        [html appendString:htmlHeader];
        [html appendString:htmlBody];
        [html appendString:htmlFooter];
        
        [self.webView loadHTMLString:html baseURL:nil];
    }
    @catch (NSException *exception) {
        DLog(@"%@",exception);
    }
    @finally {
        
    }
    
}

//品牌工艺
-(void)refreshBrand{
    
    if(self.Technology){
    
        [self.webView loadHTMLString:self.Technology baseURL:nil];
    }else{
        TaskWithGetGoodsTechnology *task = [[TaskWithGetGoodsTechnology alloc] init];
        task.delegate = self;
        task.goodid = self.goodDetail.id;
        [task start];
    }
}

//买家评论
-(void)refreshComments{
    if(self.comments){
        [self.webView loadHTMLString:self.comments baseURL:nil];
    }else{
        TaskWithGetGoodsComments *com = [[TaskWithGetGoodsComments alloc] init];
        com.goodid = self.goodDetail.id;
        com.delegate = self;
        [com start];
    }
}

#pragma mark - btn actions -

- (IBAction)on_btn_favourite:(UIButton *)sender {
    [MBProgressHUD showHUDAddedTo:self.superview animated:YES];
    TaskWithFavourite *f = [[TaskWithFavourite alloc] init];
    f.delegate = self;
    f.goodid = self.goodDetail.id;
    [f start];
}

- (IBAction)on_btn_share:(UIButton *)sender {

    [MBProgressHUD showHUDAddedTo:self.superview animated:YES];
    TaskWithShare *share = [[TaskWithShare alloc] init];
    share.goodid = self.goodDetail.id;
    share.delegate = self;
    [share start];
}

- (IBAction)on_btn_buy:(UIButton *)sender {
    [delegate commodityBuyBtn];
}

- (IBAction)on_btn_commodity_info:(UIButton *)sender {
    [self promptyAnimationIndex:0];
    [self refreshGoodIntro];
}

- (IBAction)on_btn_brand_info:(UIButton *)sender {
    [self promptyAnimationIndex:1];
    [self refreshBrand];
}

- (IBAction)on_btn_comments:(UIButton *)sender {
    [self promptyAnimationIndex:2];
    [self refreshComments];
}

-(void)promptyAnimationIndex:(int)index{
    CGRect frame = self.promptyBg.frame;
    frame.origin.x = 20 + index * 100;
    
    [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.promptyBg.frame = frame;
    } completion:NULL];
}

-(void)dealloc{
    
}

@end
